﻿
var bufferCount = 0;
var isLoaded = true;

var ajaxGetMiniBlog = function (ishashchange, isBufferRetry) {
    var url;
    if (!window.location.hash || window.location.hash.indexOf('#!') < 0) {
        url = window.location.href;
        if (ishashchange) {
            $(window).scrollTop(0);
            $("a[action]").removeClass("current");
            $("a[action='all']").attr("class", "current");
        }
    }
    else {
        url = window.location.href.replace(App.config.Domain, "").split('!')[1];
        if (ishashchange) {
            $(window).scrollTop(0);
            var urlJson = App.UlrToJson(url);
            $("a[action]").removeClass("current");
            if (!urlJson) $("a[action='all']").attr("class", "current");
            else {
                if (!urlJson["IsAdvanced"]) {
                    var isAll = 1;


                    for (var p in urlJson) {
                        var elem = $("a[action='" + p + "']");
                        if (elem[0]) {
                            isAll = 0;
                            elem.attr("class", "current");
                        }
                    }
                    if (isAll)
                        $("a[action='all']").attr("class", "current");
                    else
                        $("a[action='all']").removeClass("current");
                }
            }
        }
    }
    var loadMB = $("#loadMB");
    var loadError = $("#loadError");
    var loadNoResult = $("#loadNoresult");

    var loadMB1 = $("#loadMB1");
    var loadError1 = $("#loadError1");
    var loadNoResult1 = $("#loadNoresult1");
    if (ishashchange) {
        bufferCount = 0;
        loadMB.show();
        loadError.hide();
        loadNoResult.hide();
    }
    else {
        if (!isBufferRetry)
            bufferCount++;
        loadMB1.show();
        loadError1.hide();
        loadNoResult1.hide();
    }
    url = App.AddQueryToUrl(url.replace(/#/g, ""), "Buffer", bufferCount);
    isLoaded = false;
    $.ajax({
        dataType: "json",
        url: url,
        cache: false,
        type: "get",
        success: function (o) {
            if (o.Code == "A00005") {
                if (ishashchange) {
                    loadMB.hide();
                    $(".MIB_feed").remove();
                    $("#page").remove();
                    $(o.Data).insertAfter(loadNoResult);
                    App.initMedia();
                    App.initNameCard($("#feed_list"));
                    loadMB1.remove();
                    loadNoResult1.remove();
                    loadError1.remove();
                }
                else {
                    $(o.Data).insertAfter(loadNoResult1);
                    App.initMedia("feed_list" + bufferCount);
                    App.initNameCard($("#feed_list" + bufferCount));
                    loadMB1.remove();
                    loadNoResult1.remove();
                    loadError1.remove();
                }
            }
            else {
                if (ishashchange) {
                    loadMB.hide();
                    loadNoResult.show();
                }
                else {
                    loadMB1.hide();
                    loadNoResult1.show();
                }
            }
            isLoaded = true;
        },
        error: function () {
            if (ishashchange) {
                loadMB.hide();
                loadNoResult.hide();
                loadError.show();
            }
            else {
                loadMB1.hide();
                loadNoResult1.hide();
                loadError1.show();
            }
            isLoaded = true;
        }
    });
};
$(document).ready(function () {
    App.TimerFunArray.push(App.ajaxAtWho);
    App.StartTimer();
    App.bindGoTop(curBorder);
    $(window).hashchange(ajaxGetMiniBlog);
    ajaxGetMiniBlog(true);
    $(window).scroll(function () {
        if ($("#loadMB1")[0]) {
            if ($(this).scrollTop() + $(this).height() > $("#loadMB1").offset().top) {
                if (bufferCount < 3 && isLoaded) {
                    ajaxGetMiniBlog();
                }
            }
        }
    });
});
window.onload = function () {
    var voteItems = $("div[type='vote']");
    for (var i = 0; i < voteItems.length; i++) {
        var img = $(voteItems[i]).find("img")[0];
        var img1 = $(voteItems[i]).find("img")[1];

        $(img1).css("left", parseInt(($(img).width() - 33) / 2)).css("top", parseInt(($(img).height() - 33) / 2)).css("position", "absolute");
        $(img1).show();

    }
};

var follow = function (id, name, alink) {
    if (alink.locked)
        return;
    alink.locked = true;

    App.FollowOne(id, name, function (ID, Name, O) {
        if (O.Data == 1) {
            $('<div class="MIB_btn_inter lf">互相关注<span class="MIB_line_sp">|</span><a onclick="cancelFollow(' + ID + ',\'' + Name + '\',this);return false;" href="javascript:void(0)" class="MIB_linkbl"><em>取消</em></a></div>').insertAfter($(alink).parent());
            $(alink).parent().remove();
        }
        if (O.Data == 2) {
            $('<div class="MIB_btn2 lf">已关注<span class="MIB_line_sp">|</span><a onclick="cancelFollow(' + ID + ',\'' + Name + '\',this);return false;" href="javascript:void(0)" class="MIB_linkbl"><em>取消</em></a></div>').insertAfter($(alink).parent());
            $(alink).parent().remove();
        }
        $("#more_handle").fadeIn();
        $("#remark_name").fadeIn();
    }, function () { alink.locked = false; });
};
var cancelFollow = function (id, name, alink) {
    if (alink.locked)
        return;
    App.miniConfirm("是否取消关注" + name, alink, function () {

        alink.locked = true;
        App.CancelFollow(id, function (ID, O) {
            if (O.Data == 3) {
                $('<div class="lf"><a class="btn_add" onclick="follow(' + ID + ',\'' + name + '\',this);return false;" href="javascript:void(0)"><img src="/Content/Image/transparent.gif" alt="" title="已关注你" class="ico_addGrn"><em><img src="/Content/Image/transparent.gif" alt="" class="SG_icon add_icoz">&nbsp;加关注</em></a></div>').insertAfter($(alink).parent());
                $(alink).parent().remove();
            }
            if (O.Data == 4) {
                $('<div class="lf"><a onclick="follow(' + ID + ',\'' + name + '\',this);return false;" href="javascript:void(0);" class="btn_add"><img class="SG_icon" src="/Content/Image/transparent.gif" title="关注"><em>加关注</em></a></div>').insertAfter($(alink).parent());
                $(alink).parent().remove();
            }

            $("#more_handle").fadeOut();
            $("#remark_name").fadeOut();
        }, function () { alink.locked = false; });
    });

};





$("#filter_adv_show").click(function () {
    $("#filter_adv_panel").fadeIn();
    $("#filter_key_panel").hide();
    return false;
});
$("#filter_adv_hide").click(function () {

    $("#filter_adv_panel").fadeOut(function () { $("#filter_key_panel").show(); });
    return false;
});
$("#filter_mkey_input").JQP_HighLineInput2({ nullCon: "搜索TA的话" }).keyup(function (e) {
    if (e.which == 13) {
        if (this.value && this.value != "搜索TA的话") {
            var url;
            if (window.location.href.indexOf("#!") >= 0) {
                url = "#!" + window.location.href.replace(App.config.Domain, "").split('!')[1];
            }
            else {
                url = "#!" + window.location.href.replace(App.config.Domain, "");
            }
            url = App.RemoveQueryFromUrl(window.location.href, "page");
            window.location.href = App.AddQueryToUrl(url, "Key", encodeURIComponent($.trim($(this).val()))); ;
        }
    }
});
var advSearch = function () {
    var url;
    if (window.location.href.indexOf("#!") >= 0) {
        url = "#!" + window.location.href.replace(App.config.Domain, "").split('!')[1];
    }
    else {
        url = "#!" + window.location.href.replace(App.config.Domain, "");
    }
    url = App.RemoveQueryFromUrl(window.location.href, "page");
    url = App.AddQueryToUrl(url, "IsAdvanced", "1");
    if (App.E("filter_ori").checked) {
        url = App.AddQueryToUrl(url, "IsOri", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsOri");
    }
    if (App.E("filter_ret").checked) {
        url = App.AddQueryToUrl(url, "IsRet", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsRet");
    }
    if (App.E("filter_text").checked) {
        url = App.AddQueryToUrl(url, "IsHaveLink", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsHaveLink");
    }
    if (App.E("filter_pic").checked) {
        url = App.AddQueryToUrl(url, "IsHavePic", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsHavePic");
    }
    if (App.E("filter_video").checked) {
        url = App.AddQueryToUrl(url, "IsHaveVideo", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsHaveVideo");
    }
    if (App.E("filter_music").checked) {
        url = App.AddQueryToUrl(url, "IsHaveMusic", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsHaveMusic");
    }
    if (App.E("filter_vote").checked) {
        url = App.AddQueryToUrl(url, "IsHaveVote", "1");
    }
    else {
        url = App.RemoveQueryFromUrl(url, "IsHaveVote");
    }

    if (App.E("filter_adv_input").value && App.E("filter_adv_input").value != "搜索TA的话") {
        url = App.AddQueryToUrl(url, "Key", encodeURIComponent($.trim(App.E("filter_adv_input").value)));
    }
    else {
        url = App.RemoveQueryFromUrl(url, "Key");
    }
    if (App.E("filter_adv_stime").value && App.E("filter_adv_stime").value != "选择日期") {
        url = App.AddQueryToUrl(url, "StartTime", App.E("filter_adv_stime").value);
    }
    else {
        url = App.RemoveQueryFromUrl(url, "StartTime");
    }
    if (App.E("filter_adv_etime").value && App.E("filter_adv_etime").value != "选择日期") {
        url = App.AddQueryToUrl(url, "EndTime", App.E("filter_adv_etime").value);
    }
    else {
        url = App.RemoveQueryFromUrl(url, "EndTime");
    }
    window.location.href = url;
}
$("#filter_adv_input").JQP_HighLineInput2({ nullCon: "搜索TA的话" }).keyup(function (e) {
    if (e.which == 13) {
        advSearch();
    }
});
$("#filter_adv_stime").JQP_HighLineInput2({ nullCon: "选择日期" }).JQP_DatePicker({ curDate: new Date() });
$("#filter_adv_etime").JQP_DatePicker({ curDate: new Date() });

$("#filter_mkey_btn").click(function () {
    if ($("#filter_mkey_input").val() != "搜索TA的话") {
        var url;
        if (window.location.href.indexOf("#!") >= 0) {
            url = "#!" + window.location.href.replace(App.config.Domain, "").split('!')[1];
        }
        else {
            url = "#!" + window.location.href.replace(App.config.Domain, "");
        }
        url = App.RemoveQueryFromUrl(window.location.href, "page");
        window.location.href = App.AddQueryToUrl(url, "Key", encodeURIComponent($.trim($("#filter_mkey_input").val())));
    }
    return false;
});
$("#filter_adv_btn").click(function () {
    advSearch();
    return false;
});
            